import axios, { type AxiosRequestConfig } from 'axios';
import { ElMessage } from 'element-plus';


const service = axios.create({
	baseURL: 'http://localhost:8080',
	timeout: 300 * 1000, // 请求超时时间设置为300秒
	withCredentials: true,// true允许跨域
});
// axios.defaults.timeout = 5000; // 超时时间设置
// axios.defaults.withCredentials = true; // true允许跨域
// axios.defaults.baseURL = 'http://localhost:8080';
// Content-Type 响应头
service.defaults.headers.post["Content-Type"] = "application/x-www-form-urlencoded;charset=UTF-8";

// 请求拦截器
service.interceptors.request.use(
	(config: AxiosRequestConfig | any) => {
		config.params = {
			...config.params,
			//timestamp: Date.now(),对于每一个即将发送的请求，都会在其params对象上添加一个timestamp属性，这里暂时不修改
		};
		return config;
	},
	(err) => Promise.reject(err)
);

// 响应拦截器
service.interceptors.response.use(
	(response) => {
		// 如果返回的状态码为200，说明接口请求成功，可以正常拿到数据
    	// 否则的话抛出错误
		// 这里具体怎么写看情况
		if (response.status === 200) {
			return Promise.resolve(response);
		  } else {			
			return Promise.reject(response);
		}
		//return response;
	},
	(error) => {
		let message = '';
		console.log(error);
		
		const status = error.response.status;
		switch (status) {
			// case 301:
			// 	message = 'Cookie过期，请重新登录';
			// 	localStorage.removeItem('cookie');
			// 	localStorage.removeItem('USER');
			// 	location.reload();
			// 	break;
			case 401:
				message = 'Token过期';
				break;
			case 403:
				message = '无权访问';
				break;
			case 404:
				message = '请求地址错误';
				break;
			case 405:
				message = '操作频繁，请稍候再试';
				break;
			case 500:
				message = '服务器出现问题';
				break;
			default:
				message = error.response.data.message;
				break;
		}
		// 提示错误信息
		ElMessage.error(message);
		return Promise.reject(error);
	}
);

// 类型定义
interface IHttp {
	get<T>(url: string, params?: unknown): Promise<T>;
	post<T>(url: string, params?: unknown): Promise<T>;
	posts<T>(url: string, params?: unknown): Promise<T>;
	delete<T>(url: string, params?: unknown): Promise<T>;
	put<T>(url: string, params?: unknown): Promise<T>;
}
const http: IHttp = {
	get(url, params) {
		return new Promise((resolve, reject) => {
			service
				.get(url, {
					params,
				})
				.then((res) => {
					resolve(res.data);
				})
				.catch((err) => {
					reject(err);
				});
		});
	},
	post(url, params) {
		return new Promise((resolve, reject) => {
			service.defaults.headers.post["Content-Type"] = "application/x-www-form-urlencoded;charset=UTF-8";
			service
				.post(url,params)
				.then((res) => {
					resolve(res.data);
				})
				.catch((err) => {
					reject(err);
				});
		});
	},
	posts(url, params) {
		return new Promise((resolve, reject) => {
			service.defaults.headers.post["Content-Type"] = "application/json;charset=UTF-8";
			service
				.post(url,params)
				.then((res) => {
					resolve(res.data);
				})
				.catch((err) => {
					reject(err);
				});
		});
	},
	delete(url, params) {
		return new Promise((resolve, reject) => {
			service
				.delete(url,{
					params,
				})
				.then((res) => {
					resolve(res.data);
				})
				.catch((err) => {
					reject(err);
				});
		});
	},
	//更新转化为对象
	put(url, params={}) {
		return new Promise((resolve, reject) => {
			service
				.put(url, params)
				.then((res) => {
					resolve(res.data);
				})
				.catch((err) => {
					reject(err);
				});
		});
	},
};

// 对外暴露为axios
export default http;
